package com.sky.controller.user;

import com.sky.dto.AddressSetdefault;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.impl.AddressBookServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("UserAddressController")
@Slf4j
@Api(tags = "地址薄模块")
@RequestMapping("/user/addressBook")
public class AdressController {
    @Autowired
    private AddressBookServiceImpl addressBookService;
    @PostMapping
    @ApiOperation("新增地址")
    public Result add(@RequestBody AddressBook book){
        addressBookService.add(book);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("获取用户的所有地址")
    public Result getAlladresses(){
        List<AddressBook> list = addressBookService.getAlladresses();
        return Result.success(list);
    }
    @GetMapping("/default")
    @ApiOperation("获取用户的默认地址")
    public Result getdefault(){
        return Result.success(addressBookService.getdefault());
    }
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setdefault(@RequestBody AddressSetdefault dto){
        addressBookService.setdefault(dto.getId());
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据地址id获取地址信息")
    public Result getByid(@PathVariable String id){
        return Result.success(addressBookService.getByid(Integer.parseInt(id)));
    }

    @PutMapping
    @ApiOperation("修改地址")
    public Result update(@RequestBody AddressBook book){
        addressBookService.update(book);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据地址id删除地址")
    public Result delete(String id){
        addressBookService.delete(Integer.parseInt(id));
        return Result.success();
    }
}
